package cn.vc.shop.controller;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.shop.entity.dto.OrderForGoodsDto;
import cn.vc.shop.service.OrderForGoodsService;
import cn.vc.shop.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单服务
 */
@RestController
@RequestMapping("/order_for_goods")
public class OrderForGoodsController {

    private final OrderForGoodsService orderForGoodsService;

    @Autowired
    public OrderForGoodsController(OrderForGoodsService orderForGoodsService) {
        this.orderForGoodsService = orderForGoodsService;
    }



    /**
     * 添加订单
     * @param commodityId
     * @return
     */
    @PostMapping
    public Result<?> insert(@RequestHeader(value = "token") String token,
                            @RequestParam("id") Long commodityId, @RequestParam Integer num) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        boolean bo = orderForGoodsService.insert(myUser.getId(), commodityId, num);
//        return resultBuilder.returnMsg(bo?1:0, null, null);
        return new Result<>(bo, null, null);
    }



    /**
     * 删除订单
     * @param token
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<?> delete(@RequestHeader(value = "token") String token,
                            @RequestParam Long id) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        boolean bo = orderForGoodsService.delete(id);
//        return resultBuilder.returnMsg(bo?1:0, null, null);
        return new Result<>(bo, null, null);
    }

    /**
     * 查询我的订单
     */
    @GetMapping("/1")
    public Result<List<OrderForGoodsDto>> selectMyGoods() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        List<OrderForGoodsDto> orderForGoodsList = orderForGoodsService.selectGoodsByUserId(myUser.getId());
        return new Result<>(1, orderForGoodsList);
    }
}
